package com.books.dao;

import com.books.model.entity.question.Answer;
import com.books.model.entity.question.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author 饿熊的咆哮
 * @Date 2021/10/30 8:23
 * @Version 1.0
 */
@Component
public class AnswerDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    public void addAnswer(Answer answer){
        mongoTemplate.save(answer);
    }

    public Answer findAnswerById(String _id){
        Query query = new Query(Criteria.where("_id").is(_id));
        Answer answer = mongoTemplate.findOne(query, Answer.class);
        return answer;
    }

    public List<Answer> findAnswerByQuestionId(String questionId){
        Query query = new Query(Criteria.where("questionId").is(questionId));
        List<Answer> answers = mongoTemplate.find(query, Answer.class);
        return answers;
    }
}
